﻿namespace BEGDC.CC.Web.EZVisit.Common
{
    #region Using Directives

    using System;
    using System.Data;
    using System.Web.UI;
    using BEGDC.CC.Web.Workflow;

    #endregion

    public partial class EZVRequestAttendee : BasePage
    {
        internal const string SessionKey = "EZVRequestAttendee_";

        private string ApplicationSessionKey
        {
            get
            {
                return this.Request.QueryString["sn"];
            }
        }

        protected int RowID
        {
            get
            {
                return Convert.ToInt32(this.Request["AttendeeId"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.uscAttendeeSearch.TitleControlName = this.txtTitle.ClientID;
            this.uscAttendeeSearch.IsPostBack = true;

            if (!this.IsPostBack)
            {
                this.InitForm();
            }
        }

        private void InitForm()
        {
            if (this.RowID < 1)
            {
                return;
            }

            var dt = this.Session[SessionKey + this.ApplicationSessionKey] as DataTable;

            if (dt == null)
            {
                return;
            }

            DataRow row = dt.Rows[this.RowID - 1];
            this.uscAttendeeSearch.EmployeeFlowID = Convert.ToInt32(row["EmployeeId"]);
            this.uscAttendeeSearch.EmployeeID = row["EmployeeNo"].ToString();
            this.uscAttendeeSearch.EmployeeName = row["EmployeeNameEN"].ToString();
            this.uscAttendeeSearch.EmployeeNameCN = row["EmployeeNameCN"].ToString();
            this.txtTitle.Text = row["EmployeeTitle"].ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.UpdateAttendeeSession())
            {
                this.Response.Write(@"<script type=""text/javascript"">window.close();window.returnValue='reload';</script>");
            }
        }

        private bool UpdateAttendeeSession()
        {
            if (this.RowID < 0)
            {
                return false;
            }

            if (string.IsNullOrEmpty(this.uscAttendeeSearch.EmployeeID))
            {
                this.Response.Write(@"<script type=""text/javascript"">alert('Please select a person!')</script>");
                return false;
            }

            var dt = this.Session[SessionKey + this.ApplicationSessionKey] as DataTable;

            if (dt == null)
            {
                return false;
            }

            int id = this.uscAttendeeSearch.EmployeeFlowID;
            string condition = string.Format("RowId <>{0} and EmployeeId={1}", this.RowID, id);
            if (dt.Select(condition).Length > 0)
            {
                this.Response.Write(@"<script type=""text/javascript"">alert('This person is already in the list!')</script>");
                return false;
            }

            DataRow row;

            if (this.RowID == 0)
            {
                row = dt.NewRow();

                row["EmployeeID"] = id;
                row["EmployeeNo"] = this.uscAttendeeSearch.EmployeeID;
                row["EmployeeNameCN"] = this.uscAttendeeSearch.EmployeeNameCN;
                row["EmployeeNameEN"] = this.uscAttendeeSearch.EmployeeName;
                row["EmployeeTitle"] = this.txtTitle.Text;
                row["CreateAt"] = DateTime.Now;
                row["CreateBy"] = this.UserInfo.NameEN;
                row["IsEnabled"] = true;

                dt.Rows.Add(row);
            }
            else if (this.RowID > 0)
            {
                row = dt.Rows[this.RowID - 1];

                row["EmployeeID"] = id;
                row["EmployeeNo"] = this.uscAttendeeSearch.EmployeeID;
                row["EmployeeNameCN"] = this.uscAttendeeSearch.EmployeeNameCN;
                row["EmployeeNameEN"] = this.uscAttendeeSearch.EmployeeName;
                row["EmployeeTitle"] = this.txtTitle.Text;
                row["UpdateAt"] = DateTime.Now;
                row["UpdateBy"] = this.UserInfo.NameEN;
                row["IsEnabled"] = true;
            }

            this.Session[SessionKey + this.ApplicationSessionKey] = SetRowID(dt);

            return true;
        }

        private static DataTable SetRowID(DataTable table)
        {
            if (table != null && table.Rows.Count > 0)
            {
                for (int i = 1; i <= table.Rows.Count; i++)
                {
                    table.Rows[i - 1]["RowId"] = i.ToString();
                }
            }

            return table;
        }
    }
}